package com.heima.search.listener;

import com.alibaba.fastjson.JSON;
import com.heima.common.constants.KafkaMessageConstants;
import com.heima.model.search.vos.SearchArticleVo;
import com.heima.search.service.ArticleSearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class SyncArticleListener {

    @Resource
    private ArticleSearchService articleSearchService;

    @KafkaListener(topics = KafkaMessageConstants.AP_ARTICLE_ES_SAVE_TOPIC,groupId = "AP_ARTICLE_ES_SAVE_TOPIC")
    public void apArticleSaveToEs(ConsumerRecord<String, String> record){
        String value = record.value();
        SearchArticleVo searchArticleVo = JSON.parseObject(value, SearchArticleVo.class);
        log.info("接收到消息，内容为：{}",value);
        if (StringUtils.isNotBlank(value)) {
            //调用新增索引库
            log.info("文章审核开始----------");
            articleSearchService.save(searchArticleVo);
            log.info("文章审核结束:{}",record);
        }
    }

}
